package 开餐馆;

import java.util.Scanner;

public class Main {
    static int N = 110;
    static int[] m = new int[N];
    static int[] p = new int[N];
    static int[] f = new int[N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- > 0) {
            int n = in.nextInt();
            int k = in.nextInt();
            for (int i = 1; i <= n; i++) {
                m[i] = in.nextInt();
            }
            for (int i = 1; i <= n; i++) {
                p[i] = in.nextInt();
            }
            for (int i = 1, j = 0, maxf = 0; i <= n; i++) {
                while (m[i] - m[j + 1] > k) {
                    j++;
                    maxf = Math.max(maxf, f[j]);
                }
                f[i] = maxf + p[i];
            }
            int res = 0;
            for (int i = 1; i <= n; i++) {
                res = Math.max(res, f[i]);
            }
            System.out.println(res);
        }
    }
}
